package com.example.administrator.wallet.view.activity.asset;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.adapter.RecordListAdapter;
import com.example.administrator.wallet.base.BaseActivity;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.MainBean;
import com.example.administrator.wallet.model.bean.RecordBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class RecordActivity extends BaseActivity {
    @Bind(R.id.finish_img)
    ImageButton finishImg;
    @Bind(R.id.view_title)
    TextView viewTitle;
    @Bind(R.id.count)
    TextView count;
    @Bind(R.id.price)
    TextView price;
    @Bind(R.id.mList)
    ListView mList;
    @Bind(R.id.mZZ)
    Button mZZ;
    @Bind(R.id.mSK)
    Button mSK;
    @Bind(R.id.activity_record)
    LinearLayout activityRecord;
    @Bind(R.id.linear)
    LinearLayout linear;
    @Bind(R.id.refresh)
    ClassicsHeader refresh;
    @Bind(R.id.smart)
    SmartRefreshLayout smart;
    private IRequestManager reqequestManager;
    private String token;
    private MainBean.DataBean.ListBean datas;
    private String address;
    private List<RecordBean.DataBean> iDatas;
    private String ethAddress;
    private String bitAddress;
    private RecordListAdapter adapter;
    private RecordBean recordBean;
    public static MainBean.DataBean.ListBean addr;


    @Override
    public void init() {
        setStatuColor();
        Intent intent = getIntent();
        datas = (MainBean.DataBean.ListBean) intent.getSerializableExtra("datas");
        addr =(MainBean.DataBean.ListBean) intent.getSerializableExtra("datas");
        finishImg.setVisibility(View.VISIBLE);
        viewTitle.setText(datas.getCoin_symbol());
        count.setText(datas.getBalance());
        price.setText("≈￥" + datas.getMoney());

        initData();
        initAdapter();

        smart.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                initData();
            }
        });
    }

    //展示adapter
    private void initAdapter() {
        adapter = new RecordListAdapter(this, iDatas);
        mList.setAdapter(adapter);
    }

    //点击adapter时
    private void OnClick() {
        adapter.getClickPst(new RecordListAdapter.OnClick() {
            @Override
            public void Click(RecordBean.DataBean recordBean) {
                Intent intent = new Intent(RecordActivity.this, TransactionDetailsActivity.class);
                intent.putExtra("tx_hash", recordBean.getTx_hash());
                intent.putExtra("coin_symbol", recordBean.getCoin_symbol());
                RecordActivity.this.startActivity(intent);
            }
        });

    }

    private void initData() {
        iDatas = new ArrayList<>();
        token = SharedPrefsUtil.getValue(this, "wallet", "token", "");
        bitAddress = SharedPrefsUtil.getValue(this, "wallet", "bit", "");
        ethAddress = SharedPrefsUtil.getValue(this, "wallet", "eth", "");
        reqequestManager = RequestFactory.getReqequestManager();


        String value = SharedPrefsUtil.getValue(RecordActivity.this, "wallet", "langage", Api.chinese);
        reqequestManager.httpPost(Api.BaseUrl + Api.currency, Api.getCurrency(token, datas.getAddr(), datas.getCoin_symbol(), value, ""), new IRequestCallback() {
            @Override
            public void onSuccess(String Response) {
                smart.finishRefresh();
                Gson mGson = new Gson();
                RecordBean recordBean = mGson.fromJson(Response, RecordBean.class);
                if (recordBean.getCode() == 200) {
                    mList.setVisibility(View.VISIBLE);
                    linear.setVisibility(View.GONE);
                    iDatas.addAll(recordBean.getData());
                    initAdapter();
                    OnClick();
                } else {
                    linear.setVisibility(View.VISIBLE);
                    mList.setVisibility(View.GONE);
                    Toast.makeText(RecordActivity.this, recordBean.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(String error) {
                Log.e("TAG--转账记录查询失败", error);
            }
        });


    }

    @Override
    public void onResume() {
        super.onResume();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_record;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0&&resultCode==200){
            smart.setEnableRefresh(true);
        }
    }

    @OnClick({R.id.finish_img, R.id.mZZ, R.id.mSK})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.finish_img:
                finish();
                break;
            case R.id.mZZ:
                Intent mIntent = new Intent(RecordActivity.this, AccountActivity.class);
                mIntent.putExtra("token", token);
                mIntent.putExtra("addr", datas);
                startActivity(mIntent);
                break;
            case R.id.mSK:
                Intent intent = new Intent(RecordActivity.this, ReceivableActivity.class);
                intent.putExtra("datas", datas.getAddr());
                startActivityForResult(intent,0);
                break;

        }
    }

}
